let KEY, BH;
layui.use(['layer', 'table', 'form'], function () {
    let table = layui.table,
        form = layui.form,
        layer = layui.layer;
    layui.layer.load(0, {shade: [0.5, '#fff']});


    /*查询所有中期检查的信息*/
    table.render({
        elem: '#medium-teacher-table'
        , toolbar: '#medium-teacher-table-toolbar'
        , height: 700
        , url: "/getPaper"
        , where: {queryKey: '', userName: USERNAME, state: '中期检查', type: 2}
        , cols: [
            [ //标题栏
                {field: 'bh', title: '编号', width: 90, align: 'center', sort: true},
                {field: 'title', title: '标题', align: 'center'},
                {field: 'sno', title: '学号', width: 150, align: 'center'},
                {field: 'sname', title: '姓名', width: 100, align: 'center'},
                {
                    field: 'title1', title: '所属学院', width: 130, align: 'center', templet: function (res) {
                        return res.title1.collegeName;
                    }
                },
                {
                    field: 'title1', title: '所属专业', width: 130, align: 'center', templet: function (res) {
                        return res.title1.majorName;
                    }
                },
                {
                    field: 'title1', title: '指导老师', width: 100, align: 'center', templet: function (res) {
                        return res.title1.tName;
                    }
                },
                {
                    field: 'title1', title: '老师工号', width: 130, align: 'center', templet: function (res) {
                        return res.title1.tno;
                    }
                },
                {
                    field: 'state', title: '论文阶段', width: 100, align: 'center', templet: function (res) {
                        if (res.state === '中期检查') {
                            return '<span style="color: #009688">中期检查</span>'
                        } else {
                            return '<span style="color: #999999">' + res.state + '</span>'
                        }

                    }
                },

                {
                    field: 'task', title: '中期检查状态', width: 100, align: 'center', templet: function (res) {
                        if (res.medium === null) {
                            return '<span style="color: red">待下达</span>'
                        } else {
                            if (res.medium.state === null || res.medium.state === '') {
                                return '<span style="color: red">待下达</span>'
                            } else if (res.medium.state === '审核中') {
                                return '<span style="color: blue">审核中</span>'
                            } else if (res.medium.state === '审核完成') {
                                return '<span style="color: #009688">审核完成</span>'
                            } else {
                                return '<span style="color: red">待下达</span>'
                            }
                        }
                    }
                },
                {
                    field: 'medium', title: '审核结果', width: 100, align: 'center', templet: function (res) {
                        if (res.medium === null) {
                            return '<span>- -</span>'
                        } else {
                            if (res.medium.isPass === null || res.medium.isPass === '') {
                                return '<span>- -</span>'
                            } else if (res.medium.isPass === '不通过') {
                                return '<span style="color: red">不通过</span>'
                            } else if (res.medium.isPass === '通过') {
                                return '<span style="color: #009688">通过</span>'
                            } else {
                                return '<span>- -</span>'
                            }
                        }
                    }
                },

                {title: '课题详情', align: 'center', width: 120, toolbar: '#task-table-tool'},
                {title: '中期检查操作', align: 'center', width: 180, toolbar: '#assignment-table-tool'},
                /*{title: '审核', align: 'center', width: 100, toolbar: '#check-table-tool'},*/
                {field: 'title1', title: '课题', width: 120, align: 'center', hide: true},
                {field: 'medium', title: '任务书', width: 100, align: 'center', hide: true},
            ]
        ]

        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [11, 22, 33]
        , limit: 11
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon" style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon" style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    //监听表头事件  监听对象  medium-teacher-table
    table.on('toolbar(medium-teacher-table)', function (obj) {

        //查询事件
        if (obj.event === 'query') {  //按关键字搜索事件
            let queryKey = $('#queryKey').val().trim(); //获取搜索的关键字
            if (queryKey === null || queryKey === '') {
                layer.msg('请输入要查询内容', {offset: '250px', time: 1500, anim: 6, area: ['200px', '50px']});
                return false;
            }
            table.reload('medium-teacher-table', {
                where: {queryKey: queryKey, userName: USERNAME, state: '中期检查', type: 2}
            });

        } else if (obj.event === 'queryAll') {  //查询全部
            table.reload('medium-teacher-table', {
                where: {queryKey: '', userName: USERNAME, state: '中期检查', type: 2}
            });
        }
    });

    /*监听操作栏事件 medium-teacher-table */
    table.on('tool(medium-teacher-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'taskDetail') {  //查看课题详情
            layer.open({
                type: 1
                , offset: 100
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情内容'
                , success: function (layero) {
                    let mask = $(".layui-layer-shade");
                    mask.appendTo(layero.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'check') {  //审核中期检查
            if (data.medium === null) {
                layer.msg('您还未给该学生中期检查', {offset: '250px', time: 1500, anim: 6});
                return false;
            } else if (data.medium.state !== "审核中") {
                layer.msg('该中期检查已经完成，你可查看详情', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            BH = data.bh;
            KEY = data.sno;
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['1040px', '800px']
                , content: $("#check-report")
                , title: '审核学生中期检查'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setUnKnowMsg(data);
                }
                , end: function () {
                    $("#check-report").css("display", 'none');
                }
            });

        } else if (obj.event === 'edit-medium') {  //编辑中期检查
            if (data.state !== '中期检查') {
                layer.msg('该学生论文不处于中期检查阶段', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            if (data.medium !== null) {
                layer.msg('该学生已完成中期检查', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['1040px', '800px']
                , content: $("#medium-box")
                , title: '给学生中期检查'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    //设置已知信息
                    $("#sNO").val(data.sno);
                    $("#major").val(data.title1.majorName);
                    $("#sName").val(data.sname);
                    $("#title").val(data.title);
                }
                , end: function () {
                    $("#medium-box").css("display", 'none');
                }
            });
        } else if (obj.event === 'query-medium') {  //查看中期检查详情
            if (data.medium === null) {
                layer.msg('您还未给该学生完成中期检查呢', {offset: '250px', time: 1500, anim: 6});
                return false;
            } else {
                layer.open({
                    type: 1
                    , offset: 0
                    , skin: 'layui-layer-rim'
                    , area: ['1040px', '800px']
                    , content: $("#query-report")
                    , title: '查看中期检查详情学生中期检查'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        /*设置回显数据*/
                        setUnKnowMsg(data);
                        $("#detail-tbody").html($("#check-tbody").html())
                    }
                    , end: function () {
                        $("#query-report").css("display", 'none');
                    }
                });
            }
        } else if (obj.event === 'upload-medium-file') {
            if (data.medium === null) {
                layer.msg('您还未给该学生中期检查呢', {offset: '250px', time: 1500, anim: 6});
                return false;
            } else if (data.medium.state === '审核完成' || data.medium.state === '审核中') {
                setUnKnowMsg(data);
                $("#detail-tbody").html($("#check-tbody").html());
                let fileName = data.sno + data.sname + '的中期检查情况';
                $("#query-report").wordExport(fileName);
            } else {
                layer.msg('该中期检查还未完成', {offset: '250px', time: 1500, anim: 6});
                return false
            }
        }
    });

    layui.layer.closeAll('loading');

    /*提交中期检查*/
    form.on('submit(add-medium-btn)', function (data) {
        let mediumData = JSON.stringify(data.field);
        console.log(mediumData);
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        $.ajax({
            url: "/addMedium",
            type: 'post',
            data: {
                mediumData: mediumData, userName: USERNAME
            },
            dataType: 'json',
            success: function (data) {  //调用接口成功
                if (data === true || data === 'true') {
                    layer.close(loadPage);
                    layer.closeAll('page');
                    layui.layer.msg('提交中期检查成功！', {offset: '300px', time: 2500, anim: 0});
                    table.reload('medium-teacher-table', {
                        where: {queryKey: '', userName: USERNAME, state: '中期检查', type: 2}
                    });
                } else {
                    layui.layer.close(loadPage);
                    layui.layer.alert('提交中期检查表成功！');
                }
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦,请稍后再试！');
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


    //通过
    $("#check-pass").click(function () {
        let result = '通过';
        checkResult(result);
    });

    //不通过审核
    $("#check-no-pass").click(function () {
        let result = '不通过';
        checkResult(result);
    });

});


//提交获取审核结果
function checkResult(result) {
    let opinion = $("#check-opinion").val().trim();
    if (opinion === null || opinion === '') {
        layui.layer.msg('请填写审核意见', {offset: '250px', time: 1500, anim: 6});
        return false;
    }
    addCheck(result, opinion, '中期检查', KEY, BH, USERNAME);
    layui.table.reload('medium-teacher-table', {
        where: {queryKey: '', userName: USERNAME, state: '中期检查', type: 2}
    });
}

//查询设置中期检查位置数据
function setUnKnowMsg(data) {
    $("#check-sName").html(data.sname);   //学生姓名
    $("#check-sNO").html(data.sno);   //学号
    $("#check-major").html(data.title1.majorName); //专业
    $("#check-title").html(data.title); //课题名称
    $("#check-isChange").html(data.medium.isChange);   //课题名称是否变化
    if (data.medium.changeReason !== null && data.medium.changeReason !== '') {
        $("#check-changeReason").html(data.medium.changeReason);   //变化原因
    }
    $("#check-isOne").html(data.medium.isOne); //是否一人一题
    $("#check-isReport").html(data.medium.isReport); //是否进行开题报告
    $("#check-isSurvey").html(data.medium.isSurvey);   //是否进行文件调研
    if (data.medium.problem !== null && data.medium.problem !== '') {
        $("#check-ques").html(data.medium.problem);   //拟解决问题：
    }
    $("#check-plan").html(data.medium.plan); //论文进度情况
    if (data.medium.lagReason !== null && data.medium.lagReason !== '') {
        $("#check-planReason").html(data.medium.lagReason); //论文延期滞后原因：
    }
    $("#check-stuManner").html(data.medium.sManner);   //学生工作态度情况
    $("#check-tManner").html(data.medium.tManner); //老师工作态度情况
    $("#check-quality").html(data.medium.quality); //中期质量
    if (data.medium.opinion !== null && data.medium.opinion !== '') {
        $("#check-opinion1").html(data.medium.opinion); //中期质量
        $("#check-isPass").html(data.medium.isPass);
    }
}




